package fight.view.armytype
{
	import com.greensock.easing.Linear;
	import com.greensock.TweenMax;
	import fight.FightC;
	import flash.display.Bitmap;
	import morn.core.components.Clip;
	import org.airmvc.Dispatcher;
	import utils.T;
	
	/**
	 * 弓箭手
	 * @author WLDragon 2014-05-06
	 */
	public class Bowman extends BaseArmy
	{
		
		public function Bowman(isAttack:Boolean, hp:int, resName:String, dir:int, attackEffect:Bitmap, callBack:Function)
		{
			super(isAttack, hp, resName, dir, attackEffect, callBack);
		}
		
		override protected function getScope():Array
		{
			var scope:Array = [ { x: px + 1, y: py }, { x: px + 2, y: py }, { x: px + 3, y: py }, { x: px + 4, y: py },
								{ x: px, y: py + 1 }, { x: px, y: py + 2 }, { x: px, y: py + 3 }, { x: px, y: py + 4 },
								{ x: px - 1, y: py }, { x: px - 2, y: py }, { x: px - 3, y: py }, { x: px - 4, y: py },
								{ x: px, y: py - 1 }, { x: px, y: py - 2 }, { x: px, y: py - 3 }, { x: px, y: py - 4 }];
			T.util.radomArray(scope);
			return scope;
		}
		
		override protected function attack(target:BaseArmy):void
		{
			arrow.frame = direction;
			arrow.x = this.x + ARROW_POSITION[direction].x;
			arrow.y = this.y + ARROW_POSITION[direction].y;
			arrow.visible = true;
			
			var s:Number;
			var endX:int;
			var endY:int;
			var pd:int;
			if (direction == 0 || direction == 2)
			{
				pd = direction == 0 ? 2 : 0;
				endX = target.x + ARROW_POSITION[pd].x;
				endY = arrow.y;
				s = Math.abs(this.x - target.x);
			}
			else
			{
				pd = direction == 1 ? 3 : 1;
				endY = target.y + ARROW_POSITION[pd].y;
				endX = arrow.x;
				s = Math.abs(this.y - target.y);
			}
			
			var t:Number = s / 300;
			
			TweenMax.to(arrow, t, {x: endX, y: endY, ease:Linear.easeNone, onComplete: showArrowComplete, onCompleteParams:[target]});
		}
		
		private function showArrowComplete(target:BaseArmy):void
		{
			arrow.visible = false;
			super.attack(target);
		}
		
		override protected function bruise(attacker:BaseArmy):void
		{
			var baseHurt:int = getFactorB(attacker.hp) * factorA * 0.01;
			hp -= baseHurt;
			
			if (hp <= 0)
			{
				var tx:int = this.x / 32;
				var ty:int = this.y / 32;
				mapData[ty][tx] = null;
				state = 5;
				
				baseHurt = baseHurt + hp;
			}
			
			Dispatcher.send(FightC.UPDATE_INFO_PANEL, this, isAttack, "bowmanNum", baseHurt);
			
			if (hp <= 0)
				parent.removeChild(this);
		}
		
		public var arrow:Clip;
		static public const ARROW_POSITION:Array = [{x: 24, y: 32}, {x: 15, y: 41}, {x: 0, y: 32}, {x: 15, y: 17}];
	}

}